package com.hxh.entity.output;

import com.hxh.entity.ProjectConfigRecord;
import com.hxh.util.Common;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * API返回参数
 */
public class ApiResult {
    /**
     * 消息内容
     */
    private String message;

    /**
     * 响应码：参考`ResultCode`
     */
    private Integer code;

    /**
     * 响应中的数据
     */
    private Object data;

    private boolean status;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    private List<ProjectConfigRecord> projectConfigRecord;

    public List<ProjectConfigRecord> getProjectConfigRecord() {
        return projectConfigRecord;
    }

    public void setProjectConfigRecord(List<ProjectConfigRecord> projectConfigRecord) {
        this.projectConfigRecord = projectConfigRecord;
    }

    public ApiResult() {
    }

    public ApiResult(Integer code, String msg, Object data, boolean status) {
        this.code = code;
        this.message = msg;
        this.data = data;
        this.status = status;
    }

    public ApiResult(Object data) {
        this.code = 200;
        this.message = "OK";
        this.data = data;
        this.status = true;
    }

    public ApiResult(String message) {
        this(200, message, null,true);
    }

    public ApiResult(String message, Integer code) {
        this.message = message;
        this.code = code;
        this.status = true;
    }

    public ApiResult(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.status = true;
    }

    public static ApiResult fail(String message) {
        return new ApiResult(400, message, null,false);
    }
    public static ApiResult fail(Exception e) {
        String message = Common.getStringNotNull(e.getMessage(),((InvocationTargetException) e).getTargetException().getMessage());
        return new ApiResult(400, message, null,false);
    }

    /***
     * 自定义错误返回码
     */
    public static ApiResult fail(Integer code, String message) {
        return new ApiResult(code, message, null,false);
    }

    public static ApiResult ok(String message) {
        return new ApiResult(200, message, null,true);
    }

    public static ApiResult ok() {
        return new ApiResult(200, "OK", null,true);
    }

    public static ApiResult build(Integer code, String msg, Object data) {
        return new ApiResult(code, msg, data,true);
    }

    public static ApiResult ok(String message, Object data) {
        return new ApiResult(200, message, data,true);
    }

    /**
     * 自定义返回码
     */
    public static ApiResult ok(Integer code, String message) {
        return new ApiResult(code, message);
    }

    /**
     * 自定义
     */
    public static ApiResult ok(Integer code, String message, Object data) {
        return new ApiResult(code, message, data,true);
    }

    public static ApiResult build(Integer code, String msg) {
        return new ApiResult(code, msg, null,true);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
